package com.ruoyi.project.jiaqin.companyinfo.controller;

import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companyinfo.domain.CompanyInfo;
import com.ruoyi.project.jiaqin.companyinfo.service.ICompanyInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业简介Controller
 *
 * @author admin
 * @date 2021-01-07
 */
@Controller
@RequestMapping("/jiaqin/companyinfo")
public class CompanyInfoController extends BaseController
{
    private String prefix = "jiaqin/companyinfo";

    @Autowired
    private ICompanyInfoService companyInfoService;
    @Autowired
    private ICompanyService iCompanyService;

    @RequiresPermissions("jiaqin:companyinfo:view")
    @GetMapping()
    public String companyinfo()
    {
        return prefix + "/companyinfo";
    }

    /**
     * 查询企业简介列表
     */
    @RequiresPermissions("jiaqin:companyinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyInfo companyInfo)
    {
        //根据系统用户ID查询公司ID
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null){
            companyInfo.setCompanyId(company.getId());
            startPage();
            List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出企业简介列表
     */
    @RequiresPermissions("jiaqin:companyinfo:export")
    @Log(title = "企业简介", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyInfo companyInfo)
    {
        List<CompanyInfo> list = companyInfoService.selectCompanyInfoList(companyInfo);
        ExcelUtil<CompanyInfo> util = new ExcelUtil<CompanyInfo>(CompanyInfo.class);
        return util.exportExcel(list, "companyinfo");
    }

    /**
     * 新增企业简介
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            mmap.put("company",company);
            return prefix + "/addme";
        }
        return prefix + "/add";
    }

    /**
     * 新增保存企业简介
     */
    @RequiresPermissions("jiaqin:companyinfo:add")
    @Log(title = "企业简介", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyInfo companyInfo)
    {
        //查公司是否存在
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company!=null){
            //存在的话，查企业的简介条数条数超过一不能添加
            if(companyInfoService.selectCompanyInfoCountByCompanyId(company.getId())>0){
                return AjaxResult.error("已经有企业简介了");
            }
        }
        return toAjax(companyInfoService.insertCompanyInfo(companyInfo));
    }

    /**
     * 修改企业简介
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyInfo companyInfo = companyInfoService.selectCompanyInfoById(id);
        mmap.put("companyInfo", companyInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业简介
     */
    @RequiresPermissions("jiaqin:companyinfo:edit")
    @Log(title = "企业简介", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyInfo companyInfo)
    {
        return toAjax(companyInfoService.updateCompanyInfo(companyInfo));
    }

    /**
     * 删除企业简介
     */
    @RequiresPermissions("jiaqin:companyinfo:remove")
    @Log(title = "企业简介", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyInfoService.deleteCompanyInfoByIds(ids));
    }
}
